package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.MsgMapper;
import com.ck.ck_back.pojo.Msg;
import com.ck.ck_back.service.msgservice;
import com.ck.ck_back.utils.MsgUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class msgserviceImpl implements msgservice {
    @Resource
    private MsgMapper msgMapper;

    @Transactional
    @Override
    public int identitly_success(String openid) {
        if (openid!=null&&!openid.equals("")){
            String msg_success = MsgUtils.Identitly_Success+"'"+openid+"'";
            Msg msg = new Msg();
            msg.setUserId(openid);
            msg.setMsg(msg_success);
            msg.setMsgTime(new Date().getTime());
            int result = msgMapper.insertSelective(msg);
            if (result == 1){
                return result;
            }else {return 2;}
        }else {return 404;}
    }

    @Transactional
    @Override
    public int identitly_fail(String openid, String msg) {
        if (openid!=null&&!openid.equals("")&&msg!=null&&!msg.equals("")){
            String msg_fail = MsgUtils.Identitly_Fail+"'"+msg+"'";
            Msg message = new Msg();
            message.setUserId(openid);
            message.setMsg(msg_fail);
            message.setMsgTime(new Date().getTime());
            int result = msgMapper.insertSelective(message);
            if (result == 1){
                return result;
            }else {return 2;}
        }else {return 404;}
    }

    @Transactional
    @Override
    public int article_success(String openid,String article_title) {
        if (openid!=null&&!openid.equals("")&&article_title!=null&&!article_title.equals("")){
            String article_success = MsgUtils.Article_Success_Title+"'"+article_title+"'"+MsgUtils.Article_Success_Content;
            Msg message = new Msg();
            message.setUserId(openid);
            message.setMsg(article_success);
            message.setMsgTime(new Date().getTime());
            int result = msgMapper.insertSelective(message);
            if (result == 1){
                return result;
            }else {return 2;}
        }else {return 404;}

    }

    @Transactional
    @Override
    public int airticle_fail(String openid,String article_title, String msg) {
        if (openid!=null&&!openid.equals("")&&msg!=null&&!msg.equals("")&&article_title!=null&&!article_title.equals("")){
            String article_fail = MsgUtils.Article_Fail_Title+"'"+article_title+"'"+MsgUtils.Article_Fail_Content+msg;
            Msg message = new Msg();
            message.setUserId(openid);
            message.setMsg(article_fail);
            message.setMsgTime(new Date().getTime());
            int result = msgMapper.insertSelective(message);
            if (result == 1){
                return result;
            }else {return 2;}
        }else {return 404;}
    }

    @Override
    public List<Msg> getallmsg(String openid) {
        return msgMapper.selectByUserId(openid);
    }


}
